/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	UserFilter.java
 * 模块说明：	
 * 修改历史：
 * 2006-9-13 - lxm - 创建。
 */
package com.hd123.rumba.action.user;

import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.user.IUserMgr;
import com.hd123.rumba.user.IUserRole;
import com.hd123.rumba.user.biz.SRole;
import com.hd123.rumba.util.HashMap2;

/**
 * 用户筛选条件 | 界面数据对象
 * 
 * @author lxm
 * 
 */
public class UserFilter {

  /** 代码类似于 */
  private String codeLike = null;

  /** 名称类似于 */
  private String nameLike = null;

  /** 启用状态 */
  private Integer online = VALUE_ONLINE_ALL;
  public static final Integer VALUE_ONLINE_ALL = new Integer(0);
  public static final String CAPTION_ONLINE_ALL = "全部";
  public static final Integer VALUE_ONLINE_YES = new Integer(1);
  public static final String CAPTION_ONLINE_YES = "仅启用";
  public static final Integer VALUE_ONLINE_NO = new Integer(2);
  public static final String CAPTION_ONLINE_NO = "仅禁用";
  public static final HashMap2 ONLINEMAP = new HashMap2()
      .put2(VALUE_ONLINE_ALL, CAPTION_ONLINE_ALL).put2(VALUE_ONLINE_YES, CAPTION_ONLINE_YES).put2(
          VALUE_ONLINE_NO, CAPTION_ONLINE_NO);

  /** 拥有角色 */
  private SRole hasRole = null;

  /**
   * 输出查询条件。输出格式为javascript（RQuery控件）。
   * 
   * @param qryName
   *          RQuery控件javascript变量名。
   * @param alias
   *          组织表对应的别名。
   * @return 以字符串形式返回相应的javascript语句。
   */
  public String toJSForRQuery(String qryName, String alias) throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("var sce = new RSCE();\n");
    sb.append("sce.operator = RSCE.VALUE_OPERATOR_AND;\n");

    if (codeLike != null) {
      String condition = "\"" + alias + ".login like ?\"";
      String parameterValue = "\"" + codeLike + "\"";
      sb.append("sce.append(RSCE.cond(" + condition + ", " + parameterValue
          + ", RSCE.WC_RIGHT));\n");
    }
    if (nameLike != null) {
      String condition = "\"" + alias + ".name like ?\"";
      String parameterValue = "\"" + nameLike + "\"";
      sb
          .append("sce.append(RSCE.cond(" + condition + ", " + parameterValue
              + ", RSCE.WC_BOTH));\n");
    }
    if (!VALUE_ONLINE_ALL.equals(online)) {
      String condition = "\"" + alias + ".online = ?\"";
      String parameterValue = null;
      if (VALUE_ONLINE_YES.equals(online))
        parameterValue = "true";
      else
        parameterValue = "false";
      sb.append("sce.append(RSCE.cond(" + condition + ", " + parameterValue + "));\n");
    }
    if (hasRole != null) {
      IUserMgr userMgr = (IUserMgr) NamingMgr.getInstance().lookupMgr(IUserMgr.class);
      sb.append(qryName + ".addFrom('" + userMgr.lookupClass(IUserRole.class) + "', 'ur');\n");
      sb.append("sce.append(RSCE.cond('ur.user.uuid = u.uuid'));\n");
      sb.append("sce.append(RSCE.cond('ur.role.uuid = ?', '" + hasRole.getUuid() + "'));\n");
    }

    sb.append("if (isDefined(sce.parameters))\n");
    sb.append("  " + qryName + ".setWhere(sce);\n");
    return sb.toString();
  }

  public String getCodeLike() {
    return codeLike;
  }

  public void setCodeLike(String codeLike) {
    this.codeLike = codeLike;
  }

  public SRole getHasRole() {
    return hasRole;
  }

  public void setHasRole(SRole hasRole) {
    this.hasRole = hasRole;
  }

  public String getNameLike() {
    return nameLike;
  }

  public void setNameLike(String nameLike) {
    this.nameLike = nameLike;
  }

  public Integer getOnline() {
    return online;
  }

  public void setOnline(Integer online) {
    this.online = online;
  }

}
